//build.cpp
//author: aandriyash

#include "utils.h"

#include <stdio.h>
#include <cstdlib>
#include <cstring>

#include <iostream>

int Build(RaidConfig config) 
{
    char* pData = new char[config.blockSize*config.totalBlocks];    
    for(int i = 0; i < config.totalDisks; i++) 
    {
        char name[10];
        sprintf(name, "Disk%i", i + 1);
        FILE* pDISC_FILE = fopen(name, "wb+");
        
        if (NULL == pDISC_FILE) 
        {
            std::cout << "Can't open file Disk" << i << "\n";
            return 1;
        }
        
        fwrite(pData, sizeof(char), config.blockSize * config.totalBlocks, pDISC_FILE);
        
        fclose(pDISC_FILE);
    }
    delete[] pData;
    
    return 0;
}

int main(
    int argc, 
    char* argv[]
) 
{
    if (argc < 5) 
    {
        std::cout << "Not enough parameters\n";
        return 1;
    }
    RaidConfig raidConf;
    
    raidConf.totalDisks  = atoi(argv[1]);   
    raidConf.totalBlocks = atoi(argv[2]);
    raidConf.blockSize   = atoi(argv[3]);
    raidConf.raidLevel   = -1; 
    if((argv[4][0] == 0) && (argv[4][0] == 1))
    {
        raidConf.raidLevel = 1; 
    }
    if((argv[4][0] == 1) && (argv[4][0] == 0))
    {
        raidConf.raidLevel = 10; 
    }
    
    RETURN_IF_ERROR(raidConf.Validate());
    RETURN_IF_ERROR(raidConf.Write());
    
    int err = Build(raidConf);
    
    return err;
}







